﻿<?php

class mJob
{
	public $rows = null;

	public function __construct()
	{
		$this->Load();
	}

	private function Load()
	{
		global $DB;
		$sql = "SELECT j.id, j.title, count(uj.user_id) workers FROM jobs j ".
  				"LEFT OUTER JOIN user_jobs uj ON uj.job_id = j.id ".
  				"LEFT OUTER JOIN users u ON u.id = uj.user_id and u.dt_end is NULL ".
 				"GROUP BY j.id, j.title;";
		$this->rows = $DB->select($sql);
	}

	public static function Edit($id, $title)
	{
		global $DB;

		if($id == "0")
		{
			$sql = "INSERT INTO jobs(title) VALUES(?);";
			$job_id = $DB->query($sql, $title);
			return $job_id;
		}
		else
		{
			$sql = "UPDATE jobs SET title = ? WHERE id = ?;";
			$DB->query($sql, $title, $id);
			return $id;
		}
	}

}

